// components/recruit-station/recruit-station.js
import {Publish} from "../../models/other"
const publishModel = new Publish()
Component({
  /**
   * 组件的属性列表
   */
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      this.init()
    }
  },
  properties: {
    checkone: {
      type: Boolean
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    jobsStationCategory: [],
    checkedJobKey: '',
    checkedJobStation: '',
    checkedJobStationList: [],
    toView: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getJobsStationCategory () {
      // 获取岗位分类选项
      publishModel.getJobsStationCategory({}, res => {
        res = res.map
        function getDefaultKey() {
          return Object.keys(res)[0]
        }
        this.setData({
          jobsStationCategory: res,
          checkedJobKey: getDefaultKey()
        })
      })
    },

    selectJobStation(e) {
      // 点击选择岗位右侧分类
      const key = e.currentTarget.dataset['key']
      const checkedJobStationList = this.data.checkedJobStationList
      if (checkedJobStationList.indexOf(key) > -1) {
        checkedJobStationList.splice(checkedJobStationList.indexOf(key), 1)
      } else {
        checkedJobStationList.unshift(key)
        if (this.data.checkone) {
          // 如果只能单选
          checkedJobStationList.length = 1
        }
      }
      this.setData({
        checkedJobStation: checkedJobStationList.join(','),
        checkedJobStationList
      })
      this.triggerEvent('checked', this.data.checkedJobStation)
    },

    selectJobKey(e) {
      // 点击选择岗位左侧分类
      const key = e.currentTarget.dataset['key']
      this.setData({
        checkedJobKey: key,
        toView: e.currentTarget.id
      })
    },

    reset() {
      this.setData({
        checkedJobStation: '',
        checkedJobStationList: [],
        checkedJobKey: Object.keys(this.data.jobsStationCategory)[0]
      })
    },

    init () {
      this.getJobsStationCategory()
    }
  }
})
